package cn.xspace.wnx.data.http;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Base64;

import com.xspace.android.xafcommon.util.AppUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import cn.xspace.chatui.bean.MsgRecordEntity;
import com.xspace.android.xafcommon.util.ErrorCode;

public class File2Base64Async extends AsyncTask<Object, Void, String> {
    FileToBase64Result mCallback;

    @Override
    protected String doInBackground(Object... params) {
        MsgRecordEntity info = (MsgRecordEntity) params[0];
        mCallback = (FileToBase64Result) params[1];
        String content = "";
        String name = "";
        String path = info.mFileEntity.file_url_local;
        name = getImageName(path);
        try {
//            if (info.msg_content_type == MessageEntity.ContentType.PICTURE.toNumber()) {
//                content = Base64.encodeToString(AppUtils
//                        .readStream(new FileInputStream(new File(path))), 0);

//            } else if (info.msg_content_type == MessageEntity.ContentType.VOICE.toNumber()) {
            content = Base64.encodeToString(
                    AppUtils.readStream(new FileInputStream(path)), 0);
//            }

        } catch (FileNotFoundException e) {
            content = "";
            e.printStackTrace();
        }
//				contentType = 1;
//
//			} else {
//				content = Base64.encodeToString(
//						AppUtils.readStream(new FileInputStream(info.mContent)), 0);
//				name = getImageName(name);
//			}

        return content;
    }

    @Override
    protected void onPostExecute(String result) {
        mCallback.requestResult(ErrorCode.SUCCESS_CODE_WNX, result);
        super.onPostExecute(result);
    }

    public interface FileToBase64Result {
        void requestResult(String reCode, String content);
    }


    //		private void sendFile(MucChatMsgBean info,
//				IMucFileRequestResult fileRequestResult) throws FileNotFoundException {
//			String content;
//			String name = info.mContent;
//			int contentType = 0;
//			if (info.mContentType == ImConstants.MessageType.VOICE) {
//				contentType = 1;
//				content = Base64.encodeToString(AppUtils
//						.readStream(new FileInputStream(new File(
//								AppUtils.VOICE_ADDRESS + info.mContent))), 0);
//			} else {
//				content = Base64.encodeToString(
//						AppUtils.readStream(new FileInputStream(info.mContent)), 0);
//				name = getImageName(name);
//			}
//			new RoomUploadFileTask(fileRequestResult,info).startUploadFile(content, contentType, name);
//		}
    private String getImageName(String imageUrl) {
        String name = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);

        if (TextUtils.isEmpty(name)) {
            name = "";
        }
        return name;
    }

//			new IMucFileRequestResult() {
//
//				@Override
//				public void requestResult(int reCode, String msg, MucChatMsgBean info) {
//					// TODO Auto-generated method stub
//					try {
//						if (reCode != 0) {
//							info.mStatus = 2;
//						} else {
//
//							info.imgPath = info.mContent;
//							info.mContent = msg;
//							ChatBody body = new ChatBody();
//							body.body = info.mContent;
//							body.sender_mobile = info.mobile;
//							body.type = ImConstants.MessageType(info.mContentType);
//							Gson mGson = new Gson();
//							//sendpicture
//							info.mStatus = 1;
//						}
//					} catch (Exception e) {
//						LogUtils.e(e);
//						info.mStatus = 2;
//					} finally {
//						//mMessageDb.insertOrUpdateMsg(info);
//						//stateChanged();
//					}
//				}
//			}, info
}